﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls.DataVisualization.Charting;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabAnalysis_Tab3.xaml
    /// </summary>
    public partial class VMTabAnalysis_Tab3 : UserControl
    {
        public VMTabAnalysis_Tab3()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabAnalysis_Tab3_Loaded);

            //barChart.Palette = new System.Collections.ObjectModel.Collection<ResourceDictionary>();
            //System.Windows.Controls.DataVisualization.ResourceDictionaryCollection
            //barChart.DataContext = dataContext;
            StackPanel_Config.DataContext = VMTabAnalysis_Tab3_Config.Intance;
            RefreshChart();
            VMTabAnalysis_Tab3_Config.Intance.PropertyChanged += new PropertyChangedEventHandler(Intance_PropertyChanged);

        }

        void Intance_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //RefreshChart();
        }
        ObservableCollection<VMTabAnalysis_Tab3_Item> dataContext = new ObservableCollection<VMTabAnalysis_Tab3_Item>();
        void VMTabAnalysis_Tab3_Loaded(object sender, RoutedEventArgs e)
        {
            //RefreshChart();
            StackPanel_TaskType_LoadData();
        }
        List<CheckBox> List_CheckBox = new List<CheckBox>();
        private void StackPanel_TaskType_LoadData()
        {
            StackPanel_TaskType.Children.Clear();
            List_CheckBox.Clear();
            string[] data = RootData.Intance.TaskItemGroupOfStep4.Select(c => c.Code).ToArray();
            foreach (var d in data)
            {
                CheckBox chk = new CheckBox() { Content = d , IsChecked= true};
                StackPanel_TaskType.Children.Add(chk);
                List_CheckBox.Add(chk);
                chk.Click += new RoutedEventHandler(chk_Click);
            }
            RefreshChart();
        }

        void chk_Click(object sender, RoutedEventArgs e)
        {
            RefreshChart();
        }

        public void RefreshChart()
        {
            string[] codes = List_CheckBox.Where(c => c.IsChecked == true).Select(c => c.Content.ToString()).ToArray();
            dataContext.Clear();
            foreach (var taskGrp in RootData.Intance.TaskItemGroupOfStep4)
            {
                if (codes.Contains(taskGrp.Code))
                {
                    foreach (var task in taskGrp.Tasks_Source)
                    {
                        VMTabAnalysis_Tab3_Item newData = new VMTabAnalysis_Tab3_Item(task);
                        dataContext.Add(newData);
                    }
                }
            }
            if (barChart.DataContext == null)
            {
                barChart.DataContext = dataContext;//.OrderBy(c => c.Y);
            }            
        }
    }

    public class VMTabAnalysis_Tab3_Config : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public void SendPropertyChanged(string sProp)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public static VMTabAnalysis_Tab3_Config Intance = new VMTabAnalysis_Tab3_Config();

        #region Display_Y
        #region Display_Y_ID
        private bool _Display_Y_ID = true;
        public bool Display_Y_ID
        {
            get { return _Display_Y_ID; }
            set
            {
                if (_Display_Y_ID != value)
                {
                    _Display_Y_ID = value;
                    SendPropertyChanged("Display_Y_ID");
                }
            }
        }
        #endregion
        #region Display_Y_Name
        private bool _Display_Y_Name = false;
        public bool Display_Y_Name
        {
            get { return _Display_Y_Name; }
            set
            {
                if (_Display_Y_Name != value)
                {
                    _Display_Y_Name = value;
                    SendPropertyChanged("Display_Y_Name");
                }
            }
        }
        #endregion
        #endregion

        #region Display_X
        #region Display_X_Weight
        private bool _Display_X_Weight = true;
        public bool Display_X_Weight
        {
            get { return _Display_X_Weight; }
            set
            {
                if (_Display_X_Weight != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Weight = value;
                    SendPropertyChanged("Display_X_Weight");
                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        #region Display_X_Quantity
        private bool _Display_X_Quantity = true;
        public bool Display_X_Quantity
        {
            get { return _Display_X_Quantity; }
            set
            {
                if (_Display_X_Quantity != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Quantity = value;
                    SendPropertyChanged("Display_X_Quantity");

                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        #region Display_X_Quanlity
        private bool _Display_X_Quanlity = false;
        public bool Display_X_Quanlity
        {
            get { return _Display_X_Quanlity; }
            set
            {
                if (_Display_X_Quanlity != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Quanlity = value;
                    SendPropertyChanged("Display_X_Quanlity");
                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        #region Display_X_Systematic
        private bool _Display_X_Systematic = false;
        public bool Display_X_Systematic
        {
            get { return _Display_X_Systematic; }
            set
            {
                if (_Display_X_Systematic != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Systematic = value;
                    SendPropertyChanged("Display_X_Systematic");
                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        private void Display_X_DisableAll()
        {
            _Display_X_Weight = false;
            _Display_X_Quanlity = false;
            _Display_X_Quantity = false;
            _Display_X_Systematic = false;
        }
        private void Display_X_Raise()
        {
            SendPropertyChanged("Display_X_Weight");
            SendPropertyChanged("Display_X_Quanlity");
            SendPropertyChanged("Display_X_Quantity");
            SendPropertyChanged("Display_X_Systematic");
        }
        private void Display_X_NeedOne()
        {
            if (_Display_X_Weight == false &&
            _Display_X_Quanlity == false &&
            _Display_X_Quantity == false &&
            _Display_X_Systematic == false)
            {
                _Display_X_Weight = true;
                Display_X_Raise();
            }
        }
        #endregion
    }

    public class VMTabAnalysis_Tab3_Item : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public void SendPropertyChanged(string sProp)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public VMTabAnalysis_Tab3_Item(Step_TaskItem _Task)
        {
            Task = _Task;
            VMTabAnalysis_Tab3_Config.Intance.PropertyChanged += new PropertyChangedEventHandler(Intance_PropertyChanged);
        }

        void Intance_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SendPropertyChanged("X");
            SendPropertyChanged("Y");
            SendPropertyChanged("Size");
        }
        Step_TaskItem Task;
        public int X_Quanlity
        {
            get
            {
                return Task.Quanlity_WeightPercent.HasValue ? Task.Quanlity_WeightPercent.Value : 0;
            }
        }
        public int X_Quantity
        {
            get
            {
                return Task.Quantity_WeightPercent.HasValue ? Task.Quantity_WeightPercent.Value : 0;
            }
        }
        public int X_System
        {
            get
            {
                return Task.Systematic_WeightPercent.HasValue ? Task.Systematic_WeightPercent.Value : 0;
            }
        }
        public int X
        {
            get
            {
                return Task.Weight;//(X_Quanlity + X_Quantity + X_System) / 3.0;
            }
        }
        public int Y
        {
            get
            {
                return Task.Weight;
            }
        }
        public int Size
        {
            get { return 100; }
        }
    }

    public class VMTabAnalysis_Tab3_Item_Color_Converter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Brushes.Yellow;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
